<template>
  <div style="color: #666;font-size: 16px;">
    <el-row :gutter="170" style="margin-bottom: 60px;margin-left: 100px">
      <el-col :span="9">
        <el-card style="color: #409EFF">
          <div><i class="el-icon-user-solid" /> 用户总数</div>
          <div style="padding: 10px 0; text-align: center; font-weight: bold">
            {{total}}
          </div>
        </el-card>
      </el-col>
      <el-col :span="9">
        <el-card style="color: #f5e042">
          <div><i class="el-icon-document" />  文章总数</div>
          <div style="padding: 10px 0; text-align: center; font-weight: bold">
          {{wztotal}}
          </div>
        </el-card>
      </el-col>


    </el-row>



    <!--  数据报表-->
      <el-row>
        <el-col :span="12">
          <div id="main" style="width: 500px; height: 450px"></div>
        </el-col>

        <el-col :span="12">
          <div id="pie" style="width: 500px; height: 400px"></div>
        </el-col>
      </el-row>


  </div>
</template>

<script>
  import * as echarts from 'echarts'


export default {
  name: "Home",
  data() {
    return {
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
      total: 0,
      pageNum: 1,
      pageSize: 10,
      wztotal:0,
    }
  },
  mounted() {  // 页面元素渲染之后再触发
    var option = {
      title: {
        text: '各季度用户数量统计',
        subtext: '趋势图',
        left: 'center'
      },
      tooltip: {
        trigger: 'item'
      },
      legend: {
        orient: 'vertical',
        left: 'left'
      },
      xAxis: {
        type: 'category',
        data: ["第一季度", "第二季度", "第三季度", "第四季度"]
      },
      yAxis: {
        type: 'value'
      },
      series: [
        {
          name: "用户",  //0
          data: [],
          type: 'bar'
        },
        {
          name: "",  //1
          data: [],
          type: 'line'
        },

        {
          name: "管理员",//2
          data: [],
          type: 'bar'
        },
        {
          name: "",  //3
          data: [],
          type: 'line'
        }
      ]
    };

    // 饼图

    var pieOption = {
      title: {
        text: '各季度用户数量统计',
        subtext: '比例图',
        left: 'center'
      },
      tooltip: {
        trigger: 'item',
        formatter: '{a} <br/>{b} : {c} ({d}%)'
      },
      legend: {
        orient: 'vertical',
        left: 'left'
      },
      series: [
        {
          name: "普通用户",
          type: 'pie',
          radius: '55%',
          center: ['25%', '70%'],
          label:{            //饼图图形上的文本标签
            normal:{
              show:true,
              position:'inner', //标签的位置
              textStyle : {
                fontWeight : 300 ,
                fontSize : 14,    //文字的字体大小
                color: "#fff"
              },
              formatter:'{c}({d}%)'
            }
          },
          data: [],  // 填空
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        },
        {
          name: "管理员",
          type: 'pie',
          radius: '50%',
          center: ['75%', '50%'],
          label:{            //饼图图形上的文本标签
            normal:{
              show:true,
              position:'inner', //标签的位置
              textStyle : {
                fontWeight : 300 ,
                fontSize : 14,    //文字的字体大小
                color: "#fff"
              },
              formatter:'{d}%'
            }
          },
          data: [
            {name: "第一季度", value: 5},
            {name: "第二季度", value: 6},
            {name: "第三季度", value: 7},
            {name: "第四季度", value: 8}
          ],  // 填空
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }
      ],

    };


    var chartDom = document.getElementById('main');   //初始化
    var myChart = echarts.init(chartDom);

    var pieDom = document.getElementById('pie');
    var pieChart = echarts.init(pieDom);



    this.request.get("/echarts/members").then(res => {   //填入数据
      // 填空
      // option.xAxis.data = res.data.x
      option.series[0].data = res.data
      option.series[1].data = res.data

      option.series[2].data = res.data
      option.series[3].data = res.data
      // 数据准备完毕之后再set
      myChart.setOption(option);


      pieOption.series[0].data = [
        {name: "第一季度", value: res.data[0]},
        {name: "第二季度", value: res.data[1]},
        {name: "第三季度", value: res.data[2]},
        {name: "第四季度", value: res.data[3]},
      ]
      pieChart.setOption(pieOption)
    })
  },
  created() {
    this.load()
  },
  methods:{
    load(){
      this.request.get("/user/page", {   //统计用户数
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,

        }
      }).then(res => {
        this.tableData = res.data.records
        this.total = res.data.total

      })

      this.request.get("/articles/page", {   //统计用户数
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,

        }
      }).then(res => {
        this.tableData = res.data.records
        this.wztotal = res.data.total

      })






    }
    }

}
</script>
